/**
 * Created by yhy on 2017/6/7.
 */


import React,{Component} from 'react'


export default class AsyncComponent extends React.Component {
    static defaultProps = {
        target: null
    };

    constructor(props) {
        super(props)
        this.state = {
            asyncComponent: null
        }
    }
    static defaultProps = {
        target:null
    }

    componentDidMount() {
        const module = this.props.target
        if (!module) {
            return;
        }

        module.then(Component => {
                this.setState({
                    asyncComponent: Component.default || Component
                })
            })
            .catch(e => {
                console.log(e)
            })
    }

    render() {
        const Component = this.state.asyncComponent

        if (!Component) {
            return null
        } else {
            return <Component {...this.props}/>
        }
    }
}